import matplotlib.pyplot as plt
import numpy as np

# 读取多项式信息
filename = 'resultC.txt'
f = open(filename, 'r')
line = f.readlines()
f.close()

# 计算绘图
x = np.linspace(-1, 1, 200)
y = 1 / (1 + 25 * x * x)
plt.plot(x, y, "k:", label="f(x)")
col = ["b-", "g-", "r-", "c-"]
for i in range(3, 16, 4):
    # 绘制不同 n 值对应的多项式曲线
    coff = [float(s) for s in line[i].split()]
    n = 5 * (i // 4 + 1)
    y2 = np.zeros(200, dtype=float)
    # 计算各点多项式值
    for j in range(n - 1, -1, -1):
        y2 = coff[j] + np.multiply(y2, x)
    s = "n=" + str(n)
    plt.plot(x, y2, col[n//5-1], label=s)

# 图例和注记
plt.title('C. Interpolation for f(x) by Chebyshev node.')
plt.xlabel("x")
plt.ylabel("y")
plt.legend()

# 图片保存至指定文件夹
figurename = "../report/figure/C_Chebyshev.png"
plt.savefig(figurename)
print("Finish draw figure C.")